/*
 *  Copyright (c) 2018. AnStudio + Medical Plus Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.anstudio.app.lhasa.common.modules;

import com.anstudio.app.lhasa.common.enums.ResponseEnum;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

import static com.anstudio.app.lhasa.common.enums.ResponseEnum.RESP_UNKNOWN_ERROR;

public class Response {

    public ResponseEnum result = RESP_UNKNOWN_ERROR;
    public String body = null;

    public Response() {
    }

    public ResponseEnum getResult() {
        return result;
    }

    public Response setResult(ResponseEnum result) {
        this.result = result;
        return this;
    }

    public String getBody() {
        return body;
    }

    public Response setBody(String body) {
        this.body = body;
        return this;
    }

    public static String encode(Object obj) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(obj);
    }

    public static List<Object> decodeList(String str, Class clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, clazz);
        return mapper.readValue(str, javaType);
    }

    public static Object decode(String str, Class clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(str, clazz);
    }
}
